package com.ny.zmb.openapi.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.concurrent.CompletableFuture;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.core.http.HttpClient;
import com.aliyun.core.http.HttpMethod;
import com.aliyun.core.http.ProxyOptions;
import com.aliyun.httpcomponent.httpclient.ApacheAsyncHttpClientBuilder;
import com.aliyun.sdk.service.ocr_api20210707.models.*;
import com.aliyun.sdk.service.ocr_api20210707.*;
import com.google.gson.Gson;
import darabonba.core.RequestConfiguration;
import darabonba.core.client.ClientOverrideConfiguration;
import darabonba.core.utils.CommonUtil;
import darabonba.core.TeaPair;

//import javax.net.ssl.KeyManager;
//import javax.net.ssl.X509TrustManager;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.io.*;
import java.util.concurrent.ExecutionException;

/**
 * createAuthor : Xmw
 * createTime : 2024-9-20 15:45
 */

@RestController
@RequestMapping("/aliyun")
@CrossOrigin
public class AliyunOcrController {

    @RequestMapping("/yyzzOci")
    public String yyzzOci(
            String fileName
    ) throws FileNotFoundException, ExecutionException, InterruptedException {

        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId("LTAI5tAGDhu3zyidjXGAzQ4s")
                .accessKeySecret("Tg5gT5zXFggT6Lm74O45SitvB7SzCS")
                .build());

        // Configure the Client
        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("ocr-api.cn-hangzhou.aliyuncs.com")
                )
                .build();

        InputStream bodyStream = new FileInputStream(new File(
                "D:\\xampp\\htdocs\\zmb\\pm.zhaimobao.com\\public\\" + fileName)
        );
        RecognizeBusinessLicenseRequest recognizeBusinessLicenseRequest = RecognizeBusinessLicenseRequest.builder()
                .body(bodyStream)
                .build();

        CompletableFuture<RecognizeBusinessLicenseResponse> response = client.recognizeBusinessLicense(recognizeBusinessLicenseRequest);
        RecognizeBusinessLicenseResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));

        client.close();

        return new Gson().toJson(resp);

    }

    /**
     * 阿里云OCR 身份证识别
     * @param fileName
     * @return
     * @throws Exception
     */
    @RequestMapping("/sfzOcr")
    private String sfzOcr(
            String fileName
    ) throws Exception {


        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId("LTAI5tAGDhu3zyidjXGAzQ4s")
                .accessKeySecret("Tg5gT5zXFggT6Lm74O45SitvB7SzCS")
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("ocr-api.cn-hangzhou.aliyuncs.com")
                )
                .build();

        InputStream bodyStream = new FileInputStream(new File(
                "D:\\xampp\\htdocs\\zmb\\pm.zhaimobao.com\\public\\" + fileName)
        );

        RecognizeIdcardRequest recognizeIdcardRequest = RecognizeIdcardRequest.builder()
                .body(bodyStream)
                .build();

        CompletableFuture<RecognizeIdcardResponse> response = client.recognizeIdcard(recognizeIdcardRequest);
        RecognizeIdcardResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));

        RecognizeBusinessLicenseRequest recognizeBusinessLicenseRequest = RecognizeBusinessLicenseRequest.builder()
                .body(bodyStream)
                .build();

        client.close();

        return new Gson().toJson(resp);

    }

    /**
     * 阿里云OCR 银行卡
     * @param fileName
     * @return
     * @throws Exception
     */
    @RequestMapping("/yhkOcr")
    private String yhkOcr(
            String fileName
    ) throws Exception {


        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId("LTAI5tAGDhu3zyidjXGAzQ4s")
                .accessKeySecret("Tg5gT5zXFggT6Lm74O45SitvB7SzCS")
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("ocr-api.cn-hangzhou.aliyuncs.com")
                )
                .build();

        InputStream bodyStream = new FileInputStream(new File(
                "D:\\xampp\\htdocs\\zmb\\pm.zhaimobao.com\\public\\" + fileName)
        );

        RecognizeAllTextRequest recognizeAllTextRequest = RecognizeAllTextRequest.builder()
                .body(bodyStream)
                .type("BankCard")
                .build();

        CompletableFuture<RecognizeAllTextResponse> response = client.recognizeAllText(recognizeAllTextRequest);
        RecognizeAllTextResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));

        client.close();

        return new Gson().toJson(resp);

    }



}
